<template>
  <div class="subject">
    <el-form :model="form" class="box1" label-width="60px">
      <el-form-item label="学科编号">
        <el-input v-model="form.rid" size="mini" />
      </el-form-item>
      <el-form-item label="学科名称">
        <el-input v-model="form.name" size="mini" />
      </el-form-item>
      <el-form-item label="创建者">
        <el-input v-model="form.username" size="mini" />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="form.status" size="mini" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button @click="clear">清除</el-button>
        <el-button type="primary" @click="addClick">+新增学科</el-button>
      </el-form-item>
    </el-form>
    <el-card style="margin-top: 20px">
      <el-table :data="list" stripe style="width: 100%">
        <el-table-column
          style="font-size=2px"
          label="序号"
          type="index"
          width="100px"
        />
        <el-table-column label="学科编号" prop="id" />
        <el-table-column label="学科名称" prop="short_name" />
        <el-table-column label="学科简介" prop="remark" />
        <el-table-column label="创建者" prop="username" />
        <el-table-column label="创建日期" prop="update_time" />
        <el-table-column label="状态" prop="status">
          <template v-slot="{ row }">
            {{ row.status === 1 ? '禁用' : '启用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <!-- {{row.status===1?'禁用':'启用'}} -->
            <el-button type="text" @click="edit(row)">编辑</el-button>
            <el-button type="text" @click="qiehuan(row.id)">
              {{ row.status === 0 ? '禁用' : '启用' }}
            </el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页按钮 -->
    <template>
      <div style="text-align: center; padding-top: 30px">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 3, 5, 100, 200, 300]"
          :page-size="page.limit"
          layout="sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </template>
    <!-- 弹框组件 -->
    <Add ref="add" v-model="show" @getData="getData" />
  </div>
</template>

<script>
import { subjectList, subjectRemove, subjectStatus } from '@/api/permission'
import Add from './components/Add.vue'
export default {
  components: {
    Add
  },
  data() {
    return {
      form: {
        username: '',
        id: '',
        short_name: '',
        status: ''
      },

      show: false,
      value: '',
      list: [],

      options: [
        {
          value: '选项1',
          label: '禁用'
        },
        {
          value: '选项2',
          label: '选用'
        }
      ],
      page: {
        page: 1, // 页码
        limit: 10,
        total: 100
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 页容量切换
    handleSizeChange(limit) {
      console.log('每页', limit)
      this.page.limit = limit
      this.getData()
    },
    // 页码切换
    handleCurrentChange(page) {
      console.log('当前页', page)
      this.page.page = page
      this.getData()
    },
    //  新增弹框

    addClick() {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'add'
    },

    // 学科列表

    async getData() {
      const res = await subjectList({
        page: this.page.page,
        limit: this.page.limit
      })

      this.list = res.data.items
      this.total = res.data.pagination.total

      console.log('员工列表数据', res)
    },
    //  编辑按钮
    edit(row) {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'edit'
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    // 删除按钮
    del(id) {
      this.$confirm('傻猪猪你要删我?', '提示')
        .then(async() => {
          // 删除Api
          await subjectRemove({ id })
          this.$message.success('再见了负心人')
          if (this.page.page > 1 && this.listData.length <= 1) {
            this.page.page--
          }

          this.getData()
        })
        .catch(() => {})
    },

    //  切换状态
    async qiehuan(id) {
      await subjectStatus({ id })
      this.$message.success('修改成功')
      this.getData()
    },

    // 搜索
    async search() {
      const res = await subjectList(this.form)
      console.log('搜索', res)
      if (res.data.items.length != 0) {
        this.list = res.data.items
      } else {
        this.$message.error('找不到,你在搜什么玩意')
      }

      this.clear()
    },

    //  清空
    clear() {
      this.form = {
        username: '',
        id: '',
        short_name: '',
        status: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.subject {
  padding: 15px;
  //   background: #ccc;
  //   display: flex;
  .box1 {
    display: flex;
  }
  .block {
    text-align: center;
  }
  ::v-deep .el-form-item__label {
    font-size: 12px;
  }
}
</style>
